<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/18 0018
 * Time: 15:30
 */

namespace Admin\Controller;

use Think\Controller;
class TypeController extends Controller
{

    public function index(){
        $id=session('id');
        $adminmodel=M('admin');
        $admin=$adminmodel->where(array('id'=>$id))->select();
        $this->admin=$admin;
        $typemodel=M('type');
        $types=$typemodel->order('tid ')->select();
        $this->types=$types;
        for ($i = 0; $i < count($types); $i++){
            $types[$i]["code"] = $i + 1;
        }
        if(!$id)
        {$this->error('登录已过期!',U("index.php/Admin/Index/index"));}
        elseif($types){
            $this->assign('list', $types);
            $this->display();
        }
        elseif ($admin){
            $this->assign('admin', $admin);
            $this->display();
        }
        else{ $this->error('获取失败!', U("Admin/Type/addtype"));}

       }


    public function del(){

        $tid = $_GET['id'];
        $types = M("type");
        $types->tid = $tid;
        $types->delete();

        if ($types) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败!');

        }

    }




    public function insert(){
        $data['typename'] = trim(I('post.typename'));
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/images/pic/'; // 设置附件上传根目录
        $upload->autoSub = false;
        $info = $upload->uploadOne($_FILES['pic']);
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }
        $data['t_pic']=$info['savepath'].$info['savename'];
        $result=M('type')->where(array('typename'=>$data['typename']))->find();
        if($result)
            $this->error('类别名重复！', U("Type/addtype"));
        else{
            M('type')->add($data);
            //var_dump($data);
            $url=U('Type/index');
            header("Location: $url");
            exit(0);
        }





    }







}